package org.liaohailong.victorlibrary.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.gson.JsonObject;

import org.liaohailong.library.http.HttpService;
import org.liaohailong.library.util.PermissionUtil;
import org.liaohailong.victorlibrary.R;
import org.liaohailong.victorlibrary.base.BaseActivity;
import org.liaohailong.victorlibrary.http.HttpCallback;
import org.liaohailong.victorlibrary.ui.slide.SlideLeftActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity {

    private final List<ButtonInfo> buttonInfoList = new ArrayList<>();

    {
//        buttonInfoList.add(new ButtonInfo("演示SQLite框架", SQLiteActivity.class));
//        buttonInfoList.add(new ButtonInfo("演示下拉刷新（老版本）", RefreshActivity.class));
        buttonInfoList.add(new ButtonInfo("演示下拉刷新（新版本）", PtrActivity.class));
        buttonInfoList.add(new ButtonInfo("演示标题吸顶列表SectionRecyclerView", SectionListActivity.class));
        buttonInfoList.add(new ButtonInfo("演示ShapeImageView", ShapeImageActivity.class));
        buttonInfoList.add(new ButtonInfo("演示HeaderScrollView", HeaderScrollActivity.class));
//        buttonInfoList.add(new ButtonInfo("演示新闻评论页", NestedDetailActivity.class));
        buttonInfoList.add(new ButtonInfo("演示电商平台首页", ShoppingActivity.class));
        buttonInfoList.add(new ButtonInfo("演示网络图片库", NetImageActivity.class));
//        buttonInfoList.add(new ButtonInfo("演示横竖屏切换", OrientationChangedActivity.class));
        buttonInfoList.add(new ButtonInfo("演示美团-外卖模块导航栏", NavigationBarActivity.class));
//        buttonInfoList.add(new ButtonInfo("演示矩阵变换", MatrixActivity.class));
        buttonInfoList.add(new ButtonInfo("演示图片裁剪", CropImageEnterActivity.class));
//        buttonInfoList.add(new ButtonInfo("演示相机调用", CameraActivity.class));
        buttonInfoList.add(new ButtonInfo("演示获取视频缩略图", ThumbActivity.class));
        buttonInfoList.add(new ButtonInfo("演示WheelView", WheelViewActivity.class));
        buttonInfoList.add(new ButtonInfo("演示弹出窗", DialogActivity.class));
        buttonInfoList.add(new ButtonInfo("空指针异常", null));
        buttonInfoList.add(new ButtonInfo("演示ShapeFrameLayout", ShapeLayoutActivity.class));
        buttonInfoList.add(new ButtonInfo("演示渲染Pdf", PdfActivity.class));
        buttonInfoList.add(new ButtonInfo("演示PhotoView", PhotoViewActivity.class));
        buttonInfoList.add(new ButtonInfo("演示SlideLinearLayout", SlideLeftActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] permissions = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA
        };
        PermissionUtil.INSTANCE.requestIfNot(
                this,
                Arrays.asList(permissions),
                0);

        ViewGroup container = findViewById(R.id.ll_container);
        // 动态添加按钮
        for (ButtonInfo info : buttonInfoList) {
            Button button = new Button(this);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            button.setLayoutParams(lp);

            button.setText(info.name);
            button.setAllCaps(false);
            button.setOnClickListener((v) -> {
                Intent intent = new Intent(v.getContext(), info.clz);
                startActivity(intent);
            });

            container.addView(button);
        }


        // https://www.wanandroid.com
        // friend/json
        // 临时测试一个接口
        String url = "https://www.wanandroid.com/friend/json";
        Map<String, String> params = new HashMap<>();
        new HttpService().get(url, params, new HttpCallback<List<JsonObject>>() {

            @Override
            protected void onSuccess(List<JsonObject> data) {
                Log.i("MainActivity", "onSuccess: " + data);
            }

            @Override
            protected void onFailure(int code, String error) {
                Log.i("MainActivity", "onFailure code: " + code + ", error: " + error);
            }
        });
    }


    public final static class ButtonInfo {
        private final String name;
        private final Class<?> clz;

        public ButtonInfo(String name, Class<?> clz) {
            this.name = name;
            this.clz = clz;
        }
    }
}
